package com.lagou.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.entity.CourseComment;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 */
public interface CourseCommentDao extends BaseMapper<CourseComment> {



    /**
     * 某个课程的全部留言（分页）
     * @param courseId 课程编号
     * @param offset 起始位置，或者说数据偏移
     * @param pagesize 每页的条数
     * @return 留言集合
     */
    @Select({"SELECT\n" +
            "        id,`course_id`,`section_id`,`lesson_id`,user_id,`user_name`,`parent_id`,`is_top`,`comment`,`like_count`,`is_reply`,`type`,`status`,create_time ,update_time ,is_del,`last_operator`,`is_notify`,`mark_belong`,`replied` \n" +
            "        FROM course_comment \n" +
            "        WHERE is_del = 0\n" +
            "        AND course_id = #{courseId}\n" +
            "        ORDER BY is_top DESC , like_count DESC , create_time DESC\n" +
            "        LIMIT #{offset}, #{pageSize}"})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id" , property = "favoriteRecords", many = @Many(select = "com.lagou.mapper.CourseCommentFavoriteRecordDao.getFavorites"))
    })
    List<CourseComment> getCommentsByCourseId(@Param("courseId") Integer courseId,@Param("offset") Integer offset,@Param("pageSize") Integer pagesize);


    /**
     * 更新点赞的数量
     * @param like_count 点赞则加1，取消赞则减一
     * @param comment_id 对应用户的对应的评论id
     * @return 受影响的行数
     */
    @Update({"update course_comment set like_count = like_count + #{like_count} where id = #{comment_id}"})
    Integer updateLikeCount(@Param("like_count") Integer like_count,@Param("comment_id") Integer comment_id);
}
